<template>
  <section>
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item class="mb-10">
          <el-input size="small" v-model="filters.name" placeholder="型号名称"></el-input>
        </el-form-item>
        <el-form-item class="mb-10">
          <el-button size="small" type="danger" v-on:click="getDeviceTypes">搜索</el-button>
        </el-form-item>
        <el-form-item class="mb-10">
          <el-button size="small" type="success" @click="handleAdd">新建型号</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <el-table :data="deviceTypeData" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
      style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label='序号' min-width="60">
      </el-table-column>
      <el-table-column prop="name" label="型号名称" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="deviceType" label="编码" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="info" label="描述" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="powerTime" label="周期时间" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="powerOrder" label="参数位置" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="isPower" label="是否统计" min-width="100" sortable>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="warning" size="small" @click="handleConf(scope.$index, scope.row)">参数</el-button>
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--工具条-->
    <!--    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total"
        style="float:right;">
      </el-pagination>
    </el-col> -->

    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="型号名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="型号编码">
          <el-input v-model="editForm.deviceType" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="editForm.info" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电量统计">
          <el-switch :active-value="1" :inactive-value="0"  v-model="editForm.isPower" active-text="统计" inactive-text="不统计">
          </el-switch>
        </el-form-item>
        <el-form-item label="有功电度参数位置">
          <el-input v-model="editForm.powerOrder" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="选择时间" prop="powerTime">
          <el-time-select v-model="editForm.powerTime" @change="getTime" :picker-options="{
              start: '00:00',
              step: '01:00',
              end: '24:00'
            }" placeholder="选择时间">
          </el-time-select>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增型号" :visible.sync='addFormVisible' :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="editFormRules" ref="addForm">
        <el-form-item label="型号名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="型号编码">
          <el-input v-model="addForm.deviceType" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="addForm.info" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电量统计">
          <el-switch :active-value="1" :inactive-value="0" v-model="addForm.isPower" active-text="统计" inactive-text="不统计">
          </el-switch>
        </el-form-item>
        <el-form-item label="有功电度参数位置">
          <el-input v-model="addForm.powerOrder" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="选择时间" prop="powerTime">
          <el-time-select v-model="addForm.powerTime" @change="getTime2" :picker-options="{
              start: '00:00',
              step: '01:00',
              end: '24:00'
            }" placeholder="选择时间">
          </el-time-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="success" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


    <el-dialog title="新增参数" :visible.sync='addConfVisible' :close-on-click-modal="false">
      <el-form :model="addConfForm" label-width="80px" ref="addConfForm">
        <el-form-item label="型号名称" prop="name">
          <el-input v-model="addConfForm.name" auto-complete="off" :disabled="true"></el-input>
        </el-form-item>

        <el-table :data="deviceConfListData" highlight-current-row v-loading="listLoading" style="width: 100%;">
          <el-table-column prop="name" label="已挂载参数名称" width="300" sortable>
          </el-table-column>
        </el-table>

        <!-- <el-form-item label="挂载参数">
          <el-select v-model="confId" placeholder="请选择挂载参数">
            <el-option v-for="item in confData" :key="item.id" :label="item.title" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addConfVisible = false">取消</el-button>
        <el-button type="success" @click.native="addConfSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <div class="block">
      <el-pagination style="text-align: right;" background layout="total,prev, pager, next"
        :current-page="pageNo" @current-change="handleCurrentChange" :page-size="pageSize" :total="total"></el-pagination>
    </div>

  </section>

</template>
<script>
  export default {
    name: 'test6',
    //组件创建
    created() {},
    mounted() {
      this.getDeviceTypes();
      // this.getConfs();
    },
    data() {
      return {
        filters: {
          name: ''
        },
        // value1: true,
        timeValue: '',
        deviceTypeData: [],
        deviceConfListData: [],
        total: 0,
        page: 1,
        listLoading: false,
        sels: [], //列表选中列
        confData: [],
        confId: '',
        pageNo: 1,
        pageSize: 10,
        editFormVisible: false, //编辑界面是否显示
        editLoading: false,
        editFormRules: {
          name: [{
            required: true,
            message: '请输入型号名称',
            trigger: 'blur'
          }],
          powerTime: [{ required: true, message: '请选择时间', trigger: 'blur' }]
        },
        //编辑界面数据
        editForm: {
          name: '',
          deviceType: '',
          info: '',
          powerOrder: '',
          isPower: 1,
          powerTime: ''

        },
        //新增参数界面数据
        addConfForm: {
          conf_id: '',
          device_type_id: '',
          name: ''
        },
        addConfVisible: false,

        addFormVisible: false, //新增界面是否显示
        addLoading: false,
        //新增界面数据
        addForm: {
          name: '',
          deviceType: '',
          info: '',
          powerOrder: '',
          isPower: 1,
          powerTime: ''
        }

      }
    },
    methods: {

      //获取参数列表
      getConfs() {
        // var params = {

        // }
        // getConfItemApi(params).then((res) => {
        //   this.confData = res.data.result.records;
        // });
      },
      //获取型号列表
      getDeviceTypes() {
        var params = {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
        }
        this.api.get('/work/awbDeviceType/list', {params: params}).then((res) => {
          this.deviceTypeData = res.data.result.records;
          this.total = res.data.result.total
        });
      },
      handleCurrentChange(val) {
        this.pageNo = val;
        this.getDeviceTypes();
      },
      getTime(v){
        this.editForm.powerTime =  v + ':00'
      },
      getTime2(v){
        this.addForm.powerTime =  v + ':00'
      },
      //删除
      handleDel: function(index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          var params = {

            id: row.id
          }
          this.api.post('/work/awbDeviceType/delete', params).then((res) => {
            this.listLoading = false;
            this.$message.success('操作成功');
            this.getDeviceTypes();
          });
        }).catch(() => {

        });
      },
      //显示编辑界面
      handleEdit: function(index, row) {
        this.editFormVisible = true;
        this.editForm = Object.assign({}, row);
      },
      //显示新增界面
      handleAdd: function() {

        this.addFormVisible = true;

      },
      handleConf: function(index, row) {

        // this.addConfVisible = true;
        // this.addConfForm = Object.assign({}, row);
        this.$router.push({
          // path: '/deviceConf',

          name: 'deviceTypeConf',
          query: {
            device_type_id: row.id,
          }
        })



      },
      addConfSubmit: function() {
        this.$refs.addConfForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              // this.editLoading = true;

              var params = {

                conf_id: this.confId,
                device_type_id: this.addConfForm.id,
                order: 1,

              }
              // confBandDeviceTypeApi(params).then((res) => {

              // });

            });
          }
        });
      },

      //编辑
      editSubmit: function() {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              var params = {

                name: this.editForm.name,
                deviceType: this.editForm.deviceType,
                info: this.editForm.info,
                isPower: this.editForm.isPower,
                powerOrder: this.editForm.powerOrder,
                powerTime: this.editForm.powerTime,
                id: this.editForm.id
              }
              this.api.post('/work/awbDeviceType/edit', params).then((res) => {
                if(res){
                  this.editFormVisible = false;
                  this.$message.success('操作成功');
                  this.getDeviceTypes();
                }
                this.editLoading = false;
              });
            });
          }
        });
      },
      //新增
      addSubmit: function() {
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.addLoading = true;

              var params = {

                name: this.addForm.name,
                deviceType: this.addForm.deviceType,
                info: this.addForm.info,
                isPower: this.addForm.isPower,
                powerOrder: this.addForm.powerOrder,
                powerTime: this.addForm.powerTime,
              }
              this.api.post('/work/awbDeviceType/add', params).then((res) => {
                if(res){
                  this.addFormVisible = false;
                  this.$message.success('操作成功');
                  this.getDeviceTypes();
                }
                this.addLoading = false;
              });
            });
          }
        });
      },
      selsChange: function(sels) {
        this.sels = sels;
      },
      //批量删除
      batchRemove: function() {
        var ids = this.sels.map(item => item.id).toString();
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = {
            ids: ids
          };
          batchRemoveUser(para).then((res) => {
            this.listLoading = false;
            //NProgress.done();
            this.$message.success('操作成功');
            this.getDeviceTypes();
          });
        }).catch(() => {

        });
      }
    }
  }
</script>
<style lang="less" scoped>
</style>
